package com.goods.business.service.imp;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Resource
    private ConsumerMapper consumerMapper;


    @Override
    public Map findConsumerList(Integer pageNum, Integer pageSize, String name, String address, String contact) {
        List<Consumer> consumerList = consumerMapper.findConsumerList(name,address,contact);
        List<ConsumerVO> consumerVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(consumerList)){
            for (Consumer consumer : consumerList) {
                ConsumerVO consumerVO = new ConsumerVO();
                BeanUtils.copyProperties(consumer,consumerVO);
                consumerVOList.add(consumerVO);
            }
        }
        List<ConsumerVO> page = ListPageUtils.page(consumerVOList, pageSize, pageNum);
        int total = consumerVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);

        return map;
    }

    @Override
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
    }

    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        return consumerVO;
    }

    @Override
    public void update(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer);
    }

    @Override
    public int deleteById(Long id) {
        int i = consumerMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public List<ConsumerVO> findAll() {

        List<Consumer> consumerList = consumerMapper.selectAll();

        //转成vo
        List<ConsumerVO> consumerVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(consumerList)){
            for (Consumer consumer : consumerList) {
                ConsumerVO consumerVO = new ConsumerVO();
                BeanUtils.copyProperties(consumer,consumerVO);
                consumerVOList.add(consumerVO);
            }
        }

        return consumerVOList;
    }


}
